import { MovieDao } from '../dao/movie';
import { MusicDao } from '../dao/music';
import { SentenceDao } from '../dao/sentence';
import { NotFound } from 'lin-mizar/lin';

class Content {
  static async deleteContent (id, type) {
    switch (type) {
      case 100:
        await MovieDao.deleteMovie(id);
        break;
      case 200:
        await MusicDao.deleteMusic(id);
        break;
      case 300:
        await SentenceDao.deleteSentence(id);
        break;
      default:
        throw new NotFound({ msg: '内容不存在' });
    }
  }
  static async editContent (id, params) {
    switch (params['type']) {
      case 100:
        delete params['url'];
        await MovieDao.editMovie(id, params);
        break;
      case 200:
        await MusicDao.editMusic(id, params);
        break;
      case 300:
        delete params['url'];
        await SentenceDao.editSentence(id, params);
        break;
      default:
        throw new NotFound({ msg: '期刊内容新增异常' });
    }
  }
  static async getContentList () {
    const movie = await MovieDao.getMovieList();
    const music = await MusicDao.getMusicList();
    const sentence = await SentenceDao.getSentenceList();
    let res = [];
    res.push.apply(res, movie);
    res.push.apply(res, music);
    res.push.apply(res, sentence);
    // 按数据生成时间进行排序
    res.sort((a, b) => b.created_at.localeCompare(a.created_at));
    return res;
  }
  static async addContent (cv) {
    switch (cv['type']) {
      case 100:
        // TODO 电影
        delete cv['url'];
        await MovieDao.addMovie(cv);
        break;
      case 200:
        // TODO 音乐
        await MusicDao.addMusic(cv);
        break;
      case 300:
        // TODO 句子
        delete cv['url'];
        await SentenceDao.addSentence(cv);
        break;
      default:
        throw new NotFound({ msg: '内容类型不存在' });
    }
  }
}
export { Content as ContentService };